package com.vs.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.vs.common.SessionKeys;
import com.vs.dao.ExpensesOperation;
import com.vs.model.CustomMessage;
import com.vs.model.ExpenseForm;
import com.vs.model.ExpenseSearchForm;
import com.vs.model.ProjectForm;
import com.vs.util.Constants;

public class SearchExpensesAction extends Action {
	/*
	 * public ActionForward search(ActionMapping mapping, ActionForm form,
	 * HttpServletRequest request, HttpServletResponse response) throws
	 * Exception { String next = "";
	 * System.out.println("FetchExpensesAction search called");
	 * ExpenseSearchForm expenseSearchForm = (ExpenseSearchForm) form;
	 * 
	 * request.setAttribute("expensesList", new
	 * ExpensesOperation().getExpensesList(expenseSearchForm)); next = "search";
	 * // next = "report"; return mapping.findForward(next); }
	 * 
	 * public ActionForward export(ActionMapping mapping, ActionForm form,
	 * HttpServletRequest request, HttpServletResponse response) throws
	 * Exception { String next = "";
	 * System.out.println("FetchExpensesAction export called");
	 * ExpenseSearchForm expenseSearchForm = (ExpenseSearchForm) form;
	 * 
	 * request.setAttribute("expensesList", new
	 * ExpensesOperation().getExpensesList(expenseSearchForm)); // next =
	 * "search"; next = "report"; return mapping.findForward(next); }
	 */

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String next = "";
		System.out.println("FetchExpensesAction execute called");
		ExpenseSearchForm expenseSearchForm = (ExpenseSearchForm) form;
		Constants.log.debug(expenseSearchForm);

		ArrayList<CustomMessage> customErrors = new ArrayList<CustomMessage>();
		List<ExpenseForm> expenseList = null;

		String action = request.getParameter("action");
		Constants.log.debug("action=" + action);
		System.out.println("action=" + action);

		if (action != null && action.equals("delete")) {

			Integer expenseId = Integer.parseInt(request
					.getParameter("expenseId"));
			new ExpensesOperation().deleteExpense(expenseId);
			CustomMessage info = new CustomMessage();
			info.setMessageLevel(Constants.INFO);
			info.setMessageText("expenseId " + expenseId
					+ " is deleted successfully");
			customErrors.add(info);
			next = "search";
			expenseSearchForm.setSearchType("search");
			request.setAttribute("customErrors", customErrors);
		} else if (action != null && action.equals("select")) {
			// HttpSession session = request.getSession(false);
			// session.removeAttribute("projectName");
			// session.removeAttribute("projectId");
			// session.setAttribute("projectId",expenseSearchForm.getExpenseProjectId());
			// session.setAttribute(
			// "projectName",
			// lookUpProjectName(session,
			// expenseSearchForm.getExpenseProjectId()));
		}

		expenseList = new ExpensesOperation()
				.getExpensesList(expenseSearchForm);
		request.setAttribute("reportName",
				expenseSearchForm.getExpenseProjectName());
		request.setAttribute("expensesList", expenseList);
		
		
		if(expenseList==null || expenseList.size()==0)
		{
			CustomMessage info = new CustomMessage();
			info.setMessageLevel(Constants.INFO);
			info.setMessageText("At present, no expenses are added to the project");
			customErrors.add(info);
		}
		
		getActiveSession(request).removeAttribute("expensesList");
		getActiveSession(request).setAttribute("expensesList", expenseList);

		System.out.println("seach type=" + expenseSearchForm.getSearchType());
		try {
			if (expenseSearchForm.getSearchType().equals("search"))
				next = "search";
			else if (expenseSearchForm.getSearchType().equals("reportExcel"))
				next = "reportExcel";
			else if (expenseSearchForm.getSearchType().equals("reportPDF"))
				next = "reportPDF";
		} catch (NullPointerException e) {
			next = "search";
		}
		System.out.println("next=" + next);
		return mapping.findForward(next);

	}
	
	private HttpSession getActiveSession(HttpServletRequest request) {
		return request.getSession(false);
	}

	@SuppressWarnings({ "unchecked", "unused" })
	private String lookUpProjectName(HttpSession session, Integer projectId) {
		List<ProjectForm> plist = (List<ProjectForm>) session
				.getAttribute(SessionKeys.PROJECT_LOV);
		for (int i = 0; i < plist.size(); i++) {
			if (plist.get(i).getProjectId().equals(projectId))
				return plist.get(i).getProjectName();
		}
		return null;
	}
}
